package com.aglhz.nature.adapter;

import java.util.List;

import com.aglhz.nature.R;
import com.aglhz.nature.ListView.MyGridView;
import com.aglhz.nature.adapter.AroundAdapter.ViewHolder;
import com.aglhz.nature.modle.item.AroundData;
import com.aglhz.nature.modle.item.RecommendData;
import com.aglhz.nature.view.HomeReWebActivity;
import com.aglhz.nature.view.SecondClassificationActivity;
import com.aglhz.nature.view.SecondHomeREActivity;
import com.aglhz.nature.view.ShopListActivity;
import com.squareup.picasso.Picasso;

import android.content.Context;
import android.content.Intent;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class HomeRecommendAdapter extends BaseAdapter{
	public List<RecommendData> mData;
    public Context context; 
    private RecommendGridAdapter adapter;
	
    public HomeRecommendAdapter(List<RecommendData> mData, Context context) {
		super();
		this.mData = mData;
		this.context = context;
	}
    
    @Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mData.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int  position, View convertView, ViewGroup arg2) {
		 ViewHolder viewHolder; 
	
			 
		 
			if(null==convertView){			
				  convertView = LayoutInflater.from(context).inflate(R.layout.item_recommend_listview, null); 
				  viewHolder = new ViewHolder();
				  viewHolder.item_recommend_title = (ImageView) convertView.findViewById(R.id.item_recommend_title); 
				  viewHolder.item_recommend_introdcation = (ImageView) convertView.findViewById(R.id.item_recommend_introdcation); 
				  viewHolder.item_recommend_gridView= (MyGridView) convertView.findViewById(R.id.item_recommend_gridView); 
				  
				  viewHolder.item_recommend_title.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						Intent intent=new Intent(context,SecondHomeREActivity.class);
						intent.putExtra("homeRE1",mData.get(position).getTagId());
						context.startActivity(intent);
					}
				});
				  viewHolder.item_recommend_introdcation.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						Intent intent=new Intent(context,HomeReWebActivity.class);
						intent.putExtra("homeRE2",mData.get(position).getTraceLink());
						context.startActivity(intent);
						
					}
				});
				  
				  
				  convertView.setTag(viewHolder); 
			}else{
				viewHolder = (ViewHolder) convertView.getTag(); 
			}
			//设置图片
			
				  String titleUrl = mData.get(position).getTagImgCover();
				//picasso加载网络图片
				   Picasso.with(context)  
				   .load(titleUrl)
				   .resize(getScreenWidth(), getScreenWidth()/11)
				  
				   .into(viewHolder.item_recommend_title) ;
				   
				   String intrUrl = mData.get(position).getTraceImgCover();
					//picasso加载网络图片
					   Picasso.with(context)  
					   .load(intrUrl) 
					    .resize(getScreenWidth(), getScreenWidth()/5)
					   .into(viewHolder.item_recommend_introdcation) ;
				   
				   
			adapter=new RecommendGridAdapter(mData.get(position).getLstData(), context);	  
			 viewHolder.item_recommend_gridView.setAdapter(adapter);	 
			 
			 
//			 int screenWidth = getScreenWidth();
//			 int height=(viewHolder.item_recommend_gridView.getLayoutParams().height+viewHolder.item_recommend_title.getLayoutParams().height +viewHolder.item_recommend_introdcation.getLayoutParams().height);
//				ListView.LayoutParams p=new ListView.LayoutParams(screenWidth,height);
//				convertView.setLayoutParams(p);
			return convertView;
		}
	
		class ViewHolder 
		{   
		    public ImageView item_recommend_title;
		    public ImageView item_recommend_introdcation;
		    public MyGridView item_recommend_gridView;
		} 
		
		/**
		 * 获取屏幕宽度
		 * 
		 * @return
		 */
		protected int getScreenWidth() {
			WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
			Display display = wm.getDefaultDisplay();
			return display.getWidth();
		}

}
